package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    DishMapper dishMapper;


    @Transactional
    @Override
    public Result add(SetmealDTO setmealDTO) {
        //套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.addSetmeal(setmeal);
        //套餐 菜品关联表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.add(setmealDish);
        }
        return Result.success();
    }

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {

        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        Page<Setmeal> page = setmealMapper.page(setmealPageQueryDTO);
        List<Setmeal> result = page.getResult();
        List<SetmealVO> collect = result.stream().map(setmeal -> {
            SetmealVO setmealVO = new SetmealVO();
            BeanUtils.copyProperties(setmeal,setmealVO);
            System.out.println(setmeal);
            Long categoryId = setmeal.getCategoryId();
            String name = categoryMapper.getById(categoryId).getName();
            setmealVO.setCategoryName(name);
            return setmealVO;
        }).collect(Collectors.toList());
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(collect);
        return pageResult;
    }

    //根据id获取套餐
    public Result getById(Long id){
        Setmeal byId = setmealMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(byId,setmealVO);
        List<SetmealDish> bySetmealId = setmealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(bySetmealId);
        return Result.success(setmealVO);
    }

    @Transactional
    @Override
    public Result update(SetmealVO setmealVO) {
        if (setmealVO ==null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        setmealDishMapper.dropBySetmealId(setmealVO.getId());

        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();

        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealVO.getId());
            setmealDishMapper.add(setmealDish);
        }
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        setmealMapper.update(setmeal);
        return Result.success();
    }

    @Transactional
    @Override
    public Result deleteById(List<Long> ids) {
        if (ids==null || ids.size()==0){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }

        setmealMapper.deleteById(ids);
        for (Long id : ids) {
            setmealDishMapper.dropBySetmealId(id);
        }

        return Result.success();
    }

    @Override
    public Result updateStatus(Integer status, Long id) {
        Setmeal build = Setmeal.builder().status(status).id(id).build();
        setmealMapper.update(build);
        return Result.success();
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }


}
